package cn.yunhe.controller;


import cn.yunhe.entity.Question;
import cn.yunhe.entity.User;
import cn.yunhe.service.QuestionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class QuestionController {
    @Autowired
    @Qualifier("questionService")

    private QuestionService questionService;
    @RequestMapping("/practice-improve")
    public ModelAndView getQuestionList1(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("practice-improve");
        return mv;
    }
    @RequestMapping("/questionList")
    public List<Question> getQuestionList(){
        List<Question> questionList= questionService.getQuestionList();
        return questionList;
    }
    // 试题练习-》强化练习
    @RequestMapping("/practiceimprovedetail")
    public Map getallQuesByFieldquesTy(HttpServletRequest request, HttpSession session){
        Map map = new HashMap();
        int qyid = (int) session.getAttribute("qyid");
        int fid = (int) session.getAttribute("fid");
        int perTotle = Integer.parseInt(request.getParameter("perTotle"));
        int curr = Integer.parseInt(request.getParameter("curr"));
        PageInfo pageInfo = questionService.getAllQuesByfieldidqyid(fid,qyid,perTotle,curr);
        String qyname =  questionService.getqynameByqyId(qyid);
        String fname = questionService.selectfnameByfid(fid);
        map.put("qyid",qyid);
        map.put("fid",fid);
        map.put("qyname",qyname);
        map.put("fname",fname);
        map.put("page",pageInfo);
        map.put("allPage",pageInfo.getPages());
        return map;
    }


    @RequestMapping("/intopracticeimprove")
    public ModelAndView intopracticeimprove(HttpSession session,HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        int qyid = Integer.parseInt(request.getParameter("qyid"));
        session.setAttribute("qyid",qyid);
        mv.setViewName("practice-improve");
        return mv;
    }
    @RequestMapping("/user/questionDetail2")
    public ModelAndView questionDetail(Model model, HttpSession session, int qnum){
        List<Question> questions=new ArrayList<>();
        if(session.getAttribute("questions")==null){
            User user = (User) session.getAttribute("Curr_User");
            //将得到的试题集合放在session中
            questions =  questionService.getquestion(user.getFieldId());

            session.setAttribute("questions",questions);
        }else{
            questions= (List<Question>) session.getAttribute("questions");
        }

        Question question=new Question();
        int i=0;
        for (Question q: questions) {
            i++;
            //集合中的题目进行遍历传到前台
            if (i== qnum){
                question=q;
            }
            //答案的非空判断！
            if(question.getA()==null){
                question.setA("");
            }
            if(question.getB()==null){
                question.setB("");
            }
            if(question.getC()==null){
                question.setC("");
            }
            if(question.getD()==null){
                question.setD("");
            }

        }
        model.addAttribute("question",question);
        model.addAttribute("qnum" ,qnum);

        /*return "hhhh";*/
        ModelAndView mv=new ModelAndView();
        mv.setViewName("practice-improve-qh");
        return mv;
    }


}
